package org.linlinjava.litemall.admin.web.marketing_manage;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.linlinjava.litemall.admin.constant.HeaderConstant;
import org.linlinjava.litemall.admin.service.marketing_manage.MarketingDeductionService;
import org.linlinjava.litemall.admin.vo.marketing_manage.*;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.AgedmallDeductionRule;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.util.PageUtils;
import org.linlinjava.litemall.db.util.Query;
import org.linlinjava.litemall.db.vo.marketing_manager.DeductionStaticDataInfo;
import org.linlinjava.litemall.db.vo.marketing_manager.MarketingDeductionRuleInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Map;

/**
 * 抵扣金活动
 * @author houzb
 * @Description
 * @create 2021-08-28 14:56
 */
@RestController
@RequestMapping("/marketDeduction")
public class MarketingDeductionController {

    @Autowired
    private MarketingDeductionService marketingDeductionService;

    /**
     * @api {get} /marketDeduction/ruleList
     * @apiGroup 市场营销抵扣金
     * @apiVersion 0.1.0
     * @apiDescription 市场营销抵扣金规则列表
     * @apiSuccess {int} errno 请求状态码，0表示成功,其它参考状态码文档
     * @apiSuccess {String} errmsg 结果消息说明
     * @apiSuccess {Object} data 请求响应的数据
     * @apiSuccessExample {json} Success-Response:
     * {
     *     "errno": "请求状态码，0表示成功，其它参考状态码文档",
     *     "errmsg": "请求成功/失败说明",
     *     "data": {
     *         "citys": [{
     *             "cityName": "城市名称"
     *         }]
     *     }
     * }
     */
    @RequestMapping("/ruleList")
    public Object ruleList(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<MarketingDeductionRuleInfo> list = marketingDeductionService.list(query);
        int total = marketingDeductionService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return ResponseUtil.ok(pageUtils);
    }

    /**
     * @api {post} /marketDeduction/ruleSave
     * @apiGroup 市场营销抵扣金
     * @apiVersion 0.1.0
     * @apiDescription 添加抵扣金规则列表
     * @apiSuccess {int} errno 请求状态码，0表示成功,其它参考状态码文档
     * @apiSuccess {String} errmsg 结果消息说明
     * @apiSuccess {Object} data 请求响应的数据
     * @apiSuccessExample {json} Success-Response:
     * {
     *     "errno": "请求状态码，0表示成功，其它参考状态码文档",
     *     "errmsg": "请求成功/失败说明",
     *     "data": null
     * }
     */
    @RequestMapping("/ruleSave")
    public Object saveDeductionInfo(AgedmallDeductionRule rule) {
        int rst = marketingDeductionService.saveRule(rule);
        if(rst > 0) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail("保存失败");
    }

    /**
     * @api {post} /marketDeduction/amountStatic
     * @apiGroup 市场营销抵扣金
     * @apiVersion 0.1.0
     * @apiDescription 获取统计的抵扣金数据
     * @apiSuccess {int} errno 请求状态码，0表示成功,其它参考状态码文档
     * @apiSuccess {String} errmsg 结果消息说明
     * @apiSuccess {Object} data 请求响应的数据
     * @apiSuccessExample {json} Success-Response:
     * {
     *     "errno": "请求状态码，0表示成功，其它参考状态码文档",
     *     "errmsg": "请求成功/失败说明",
     *     "data": null
     * }
     */
    @RequestMapping("/amountStatic")
    public Object getDeductionStaticDataInfo(@RequestParam Map<String, Object> params) {
        DeductionStaticDataInfo dataInfo = marketingDeductionService.getDeductionStaticData(params);
        return ResponseUtil.ok(dataInfo);
    }

    /**
     * @api {post} /marketDeduction/offlineRule
     * @apiGroup  市场营销抵扣金
     * @apiVersion 0.1.0
     * @apiDescription 下架抵扣金规则
     * @apiSuccess {int} errno 请求状态码，0表示成功,其它参考状态码文档
     * @apiSuccess {String} errmsg 结果消息说明
     * @apiSuccess {Object} data 请求响应的数据
     * @apiSuccessExample {json} Success-Response:
     * {
     *     "errno": "请求状态码，0表示成功，其它参考状态码文档",
     *     "errmsg": "请求成功/失败说明",
     *     "data": null
     * }
     */
    @RequestMapping("/offlineRule")
    public Object offlineDeductionRuleConf(long id) {
        int rst = marketingDeductionService.offlineDeductionRuleConfRule(id);
        if(rst > 0) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail("下线失败");
    }



    //--------------抵扣金  wj-------------------


    /**
     * 新增抵扣金规则
     */
    @PostMapping("/addRule")
    public Object addRule(@RequestBody @Validated AddRuleVo addRuleVo, HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        addRuleVo.setBusiId(busiId);
        return marketingDeductionService.addRule(addRuleVo);
    }


    /**
     * 编辑抵扣金规则
     */
    @PostMapping("/editRule")
    public Object editRule(@RequestBody @Validated AddRuleVo addRuleVo, HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        addRuleVo.setBusiId(busiId);
        return marketingDeductionService.editRule(addRuleVo);
    }

    /**
     * 删除抵扣金规则 DeleteRuleVo
     */
    @PostMapping("/deleteRule")
    public Object deleteRule(@RequestBody @Validated @Size(min = 1,message = "规则id不能为空") List<Long> idList){
        return marketingDeductionService.deleteRule(idList);
    }


    /**
     * 获取抵扣金活动规则详情
     */
    @PostMapping("/detailRule")
    public Object detailRule(@RequestBody @Validated DetailRuleVo detailRuleVo){
        return marketingDeductionService.detailRule(detailRuleVo);
    }


    /**
     * 获取抵扣金活动规则详情
     */
    @PostMapping("/changeState")
    public Object changeState(@RequestBody @Validated ChangeStateVo changeStateVo){
        return marketingDeductionService.changeState(changeStateVo);
    }

    /**
     * 获取抵扣金活动规则列表
     */
    @PostMapping("/ruleList")
    public Object ruleList(@RequestBody @Validated RuleListVo ruleListVo,HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        ruleListVo.setBusiId(busiId);
        return marketingDeductionService.ruleList(ruleListVo);
    }


    /**
     * 获取抵扣金活动规则列表
     */
    @PostMapping("/summary")
    public Object summary(HttpServletRequest request){
        Subject currentUser = SecurityUtils.getSubject();
        LitemallAdmin admin = (LitemallAdmin) currentUser.getPrincipal();
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        return marketingDeductionService.summary(admin.getId(),busiId);
    }


    /**
     * 获取抵扣金活动规则用户统计列表
     */
    @PostMapping("/ruleUserStatic")
    public Object ruleUserStatic(@RequestBody @Validated RuleUserStaticVo ruleUserStaticVo,HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        ruleUserStaticVo.setBusiId(busiId);
        return marketingDeductionService.ruleUserStatic(ruleUserStaticVo);
    }



    /**
     * 获取抵扣金活动规则用户统计列表
     */
    @PostMapping("/ruleUserStaticOrderList")
    public Object ruleUserStaticOrderList(@RequestBody @Validated RuleUserStaticOrderListVo ruleUserStaticVo,HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        ruleUserStaticVo.setBusiId(busiId);
        return marketingDeductionService.ruleUserStaticOrderList(ruleUserStaticVo);
    }



}
